package com.brkj.util;

//import us.costan.chrome.ChromeView;
import android.app.Application;
import android.content.Context;

import com.brkj.model.DS_MyInfo;
import com.brkj.main.MyInfo;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import net.tsz.afinal.FinalDb;

import com.dgl.sdk.util.DBStore;
import com.dgl.sdk.util.SharePreStore;

import java.io.File;


public class MyApplication extends Application {
	//调试模式
	public static boolean isDebug = true;
	
    private static MyApplication instance;

    public static MyInfo myInfo;
    public static String myUserID;
    
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        instance = this;
        
        myInfo = new MyInfo(this);
        updateMyinfo();
        
        initImageLoader(getApplicationContext());

//        ChromeView.initialize(this);
        SharePreStore.init(this);
        DBStore.init(this, ConstAnts.BRKJ_DB);

        initFileDir();
    }

    private void initFileDir() {

       File file = new File(ConstAnts.IMG_CATCH_DIR);
        if(!file.exists()){
            file.mkdirs();
        }
    }


    public static void initImageLoader(Context context) {
		// This configuration tuning is custom. You can tune every option, you may tune some of them,
		// or you can create default configuration by
		//  ImageLoaderConfiguration.createDefault(this);
		// method.
		
		DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
        .cacheInMemory(true)
        .cacheOnDisc(true)
        .build();
		
//		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
//				.defaultDisplayImageOptions(defaultOptions)
//				.threadPriority(Thread.NORM_PRIORITY - 2)
//				.denyCacheImageMultipleSizesInMemory()
//				.discCacheFileNameGenerator(new Md5FileNameGenerator())
//				.tasksProcessingOrder(QueueProcessingType.LIFO)
//				.writeDebugLogs() // Remove for release app
//				.build();
//		// Initialize ImageLoader with configuration.
//		ImageLoader.getInstance().init(config);


        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                context)
                .threadPriority(Thread.NORM_PRIORITY - 2)// 设置线程的优先级
                .denyCacheImageMultipleSizesInMemory()// 当同一个Uri获取不同大小的图片，缓存到内存时，只缓存一个。默认会缓存多个不同的大小的相同图片
                .discCacheFileNameGenerator(new Md5FileNameGenerator())// 设置缓存文件的名字
                .discCacheFileCount(100)// 缓存文件的最大个数
                .tasksProcessingOrder(QueueProcessingType.LIFO)// 设置图片下载和显示的工作队列排序
                .build();

        // Initialize ImageLoader with configuration
        ImageLoader.getInstance().init(config);
	}
	
    public static void updateMyinfo(){ 
    	myInfo.readInfo();
    	myUserID = Integer.toString(myInfo.getUserID());
    }
    
    public static MyApplication getInstance() {
        return instance;
    }
    
    public static Context getContext() {
        return instance.getApplicationContext();
    }
}
